class EventEmitter{
  constructor() {
    this.events = {};
  }
  on(eventName, callback) {
    if (this.events[eventName]) {
      this.events[eventName].push(callback);
    } else {
      this.events[eventName] = [callback];
    }
  }
  emit(eventName, eventData) {
    this.events[eventName] && this.events[eventName].forEach(cb => cb(eventData));
  }
  off(eventName, callback) {
    if (this.events[eventName]) {
      this.events[eventName] = this.events[eventName].filter(cb=>cb!==callback)
    }
  }
  once(eventName, callback) {
    let fn = () => {
      callback();
      this.off(eventName, fn);
    }
    this.on(eventName, fn);
  }
}